package server;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import both.*;
import java.awt.event.*;
import java.awt.*;
import java.util.ArrayList;

public class ResultsTab extends JPanel implements ActionListener {

    private boolean choosing;
    private JFrame questionFrame;
    private Roster playerRoster;
    private ArrayList<Team> teamRoster;
    private GameShowServer serveruse;

    private WindowListener exitListener = new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            choosing = false;
            questionFrame.dispose();
        }
    };

    public ResultsTab(GameShowServer server) {
        serveruse=server;
        String Headers[] = {StringFormatter.formatString("Team", StringFormatter.TITLE_STR), StringFormatter.formatString("% Correct", StringFormatter.TITLE_STR)};
        choosing = false;
        playerRoster = server.getGame().getRoster();
        teamRoster = playerRoster.teamsByPercentage();

        String[][] Teams = new String[teamRoster.size()][2];

        for (int n = 0; n < teamRoster.size(); n++) {
            Teams[n][0] = (teamRoster.get(n)).getName();
            Teams[n][1] = String.valueOf((teamRoster.get(n)).percentageCorrect());
        }

        DefaultTableModel change = new DefaultTableModel(Teams, Headers);
        JTable resultTable = new JTable(change) {
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        resultTable.getTableHeader().setReorderingAllowed(false);
        resultTable.setCellSelectionEnabled(false);
        resultTable.setEnabled(false);
        JScrollPane resultSP = new JScrollPane(resultTable);
        resultSP.setPreferredSize(new Dimension(500, 250));
        add(resultSP);

        JButton resultBack = new JButton(StringFormatter.formatString("BACK", StringFormatter.BUTTON_STR, 50));
        resultBack.setActionCommand("resultBack");
        resultBack.addActionListener(this);
        if (server.getGame().getCurrentQuestionIndex() == 0) {
            resultBack.setEnabled(false);
        }
        add(resultBack);
         if((serveruse.getGame().getCurrentQuestionIndex()==serveruse.getGame().getQuestionList().size())
                &&(serveruse.getGame().getCurrentQuestionIndex()!=0)){
            remove(resultBack);
        }

        JButton resultNext = new JButton(StringFormatter.formatString("NEXT QUESTION", StringFormatter.BUTTON_STR, 110));
        if((serveruse.getGame().getCurrentQuestionIndex()==serveruse.getGame().getQuestionList().size()-1)
                &&(serveruse.getGame().getCurrentQuestionIndex()!=0)){
            resultNext.setEnabled(false);
        }
        resultNext.setActionCommand("resultNext");
        resultNext.addActionListener(this);

        add(resultNext);
        setName("results-tab");

    }

    @Override
    public void actionPerformed(ActionEvent event) {
        //Cannot open multiple quesiton windows or have the option to go forward while deciding to go back
        if ((event.getActionCommand()).equals("resultBack") && !choosing) {
            choosing = true;
            questionFrame = new JFrame("Going back?");
            questionFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            questionFrame.addWindowListener(exitListener);
            //Makes disposing the window with the 'X' allow for the windows to be open again
            JPanel questionPane = new JPanel();
            questionPane.setPreferredSize(new Dimension(575, 100));
            questionFrame.setContentPane(questionPane);
            JLabel questionLabel = new JLabel(StringFormatter.formatString("Would you like to return to the previous answer mode?", StringFormatter.ANSWER_STR));
            questionPane.add(questionLabel);
            JButton yesButton = new JButton(StringFormatter.formatString("Yes", StringFormatter.TAB_STR));
            yesButton.setActionCommand("goBack");
            yesButton.addActionListener(this);
            questionPane.add(yesButton);
            JButton noButton = new JButton(StringFormatter.formatString("No", StringFormatter.TAB_STR));
            noButton.setActionCommand("close");
            noButton.addActionListener(this);
            questionPane.add(noButton);

            questionFrame.pack();
            questionFrame.setVisible(true);
        } else if ((event.getActionCommand()).equals("resultNext") && !choosing) {
            choosing = true;
            questionFrame = new JFrame("Going forward?");
            questionFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            questionFrame.addWindowListener(exitListener);
            JPanel questionPane = new JPanel();
            questionPane.setPreferredSize(new Dimension(575, 100));
            questionFrame.setContentPane(questionPane);
            JLabel questionLabel = new JLabel(StringFormatter.formatString("Would you like to move on to the next question mode?", StringFormatter.ANSWER_STR , 375));
            questionLabel.setBorder(BorderFactory.createEmptyBorder(5, 15, 0, 0));
            questionPane.add(questionLabel);
            JButton yesButton = new JButton(StringFormatter.formatString("Yes", StringFormatter.TAB_STR));
            yesButton.setActionCommand("goNext");
            yesButton.addActionListener(this);
            questionPane.add(yesButton);
            JButton noButton = new JButton(StringFormatter.formatString("No", StringFormatter.TAB_STR));
            noButton.setActionCommand("close");
            noButton.addActionListener(this);
            questionPane.add(noButton);
            questionFrame.pack();
            questionFrame.setVisible(true);
        } else if ((event.getActionCommand()).equals("goBack")) {
            Server.changeGameTabBack();
            questionFrame.dispose();
        } else if ((event.getActionCommand()).equals("goNext")) {
            Server.changeGameTab();
            questionFrame.dispose();
        } else if ((event.getActionCommand()).equals("close")) {
            choosing = false;
            questionFrame.dispose();
        }
    }
}
